package com.xk857.simpleeffect.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.db.PersonDataBase;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Customer;
import com.xk857.simpleeffect.domain.param.UpdateMeParam;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.toast.XToast;

import java.lang.reflect.Type;
import java.util.function.Consumer;

public class UpdatePasswordActivity extends AppCompatActivity implements TextWatcher {

    private TitleBar titleBar;
    private EditText passwordEdit;
    private EditText enterPasswordEdit;
    private Button updateButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        initView();
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        passwordEdit = findViewById(R.id.password_edit);
        enterPasswordEdit = findViewById(R.id.enter_password_edit);

        updateButton = findViewById(R.id.update_button);

        passwordEdit.addTextChangedListener(this);
        enterPasswordEdit.addTextChangedListener(this);

        updateButton.setOnClickListener(v -> {
            if (passwordEdit.getText().toString().length() < 6) {
                XToast.info(this, "密码不能小于6位").show();
                return;
            }
            if (!enterPasswordEdit.getText().toString().equals(passwordEdit.getText().toString())) {
                XToast.error(this, "密码与确认密码不一致，请检查后重试").show();
                return;
            }
            UpdateMeParam param = new UpdateMeParam();
            param.setPassword(passwordEdit.getText().toString());
            Type type = new TypeToken<Result<Customer>>() {
            }.getType();
            OkHttpUtils.getInstance().put(this, RequestEnum.UPDATE_PERSON_ME, type, param, true, (Consumer<Customer>) customer -> {
                runOnUiThread(() -> {
                    PersonDataBase.setPersonInfo(this, customer);
                    new MaterialDialog.Builder(this)
                            .iconRes(R.drawable.icon_info)
                            .title(R.string.info)
                            .content("修改成功，下次登录请使用新密码")
                            .onPositive((dialog, which) -> finish())
                            .positiveText(R.string.yes)
                            .show();
                });
            });
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (passwordEdit.getText().toString().length() < 6 || enterPasswordEdit.getText().toString().length() < 6) {
            updateButton.setEnabled(false);
        } else {
            updateButton.setEnabled(true);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}